#include "dialog.h"
#include "ui_dialog.h"

#include <QSettings>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    QSettings ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);

    const auto infos = QSerialPortInfo::availablePorts();
    for (const QSerialPortInfo &info : infos)
        ui->ComPortNameList->addItem(info.portName());
    ui->ComPortNameList->setCurrentText(ini.value("sys/com", "COM1").toString());
    ui->spinBox->setValue(ini.value("sys/axisY", 2).toInt());
}

QString Dialog::getComName()
{
    return ui->ComPortNameList->currentText();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_buttonBox_accepted()
{
    QSettings ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
    ini.setValue("sys/com", ui->ComPortNameList->currentText());
    ini.setValue("sys/axisY", ui->spinBox->value());
    emit changeComed(ui->ComPortNameList->currentText(), ui->spinBox->value());
}

